<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" > 

<html>
<head>
<base href="<%=basePath%>">
<title>邀请码列表</title>
<link rel="stylesheet" type="text/css" href="./js/bootstrap-3.3.5-dist/css/bootstrap.css" />
<link href="./js/metisMenu/metisMenu.min.css" rel="stylesheet">
<link href="./css/sb-admin/sb-admin-2.css" rel="stylesheet">
<link rel="stylesheet" href="./js/font-awesome/css/font-awesome.min.css">
<link href="./css/base.css" rel="stylesheet">
<link rel="stylesheet" href="./js/bootstrap-table/bootstrap-table.min.css">
<link rel="stylesheet" href="./js/bootstrap-multiselect/bootstrap-multiselect.css"/>
<link rel="stylesheet" href="./js/bootstrap-3.3.5-dist/css/bootstrap-datetimepicker.css">
<script type="text/javascript" src="./js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="./js/bootstrap-3.3.5-dist/js/bootstrap.js"></script>
<script src="./js/bootstrap-table/bootstrap-table-all.js"></script>
<script src="./js/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
<script type="text/javascript" src="./js/metisMenu/metisMenu.js"></script>
<script src="./js/bootstrap-multiselect/bootstrap-multiselect.js"></script>
<script src="./js/bootstrap-3.3.5-dist/js/bootstrap-datetimepicker.js"></script>
<script src="./js/bootstrap-3.3.5-dist/js/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="./js/bootstrap-jquery-plugin/jquery.bootstrap.js"></script>
<script type="text/javascript" src="./js/common.js"></script>
<script type="text/javascript" src="./js/xss.js"></script>
</head>
<body>
<jsp:include page="../template/head.jsp" />
<jsp:include page="../template/leftnav.jsp" />
<div id="page-wrapper">
	<div>
		<h1>邀请码列表</h1>
	</div>
	<div class="content">
		<form class="form-inline">
			<div class="form-group">
				<label for="userCode">会员ID：</label>
				<input type="text" id="userCode" class="form-control" placeholder="请输入会员ID" onkeyup="filters(this)" onchange="filters(this)">
			</div>
			<div class="form-group">
				<label for="userName">会员名称：</label>
				<input type="text" id="userName" class="form-control" placeholder="请输入会员名称" onkeyup="filters(this)" onchange="filters(this)">
			</div>
			<div class="form-group">
				<label for="time">创建时间：</label>
				<input class="form-control" type="text" id="startTime" placeholder="请输入开始的时间">
				<span>到</span>
				<input class="form-control" type="text" id="endTime" placeholder="请输入结束的时间">
				<button class="btn  search-btn" id="search">搜索</button>
				<button id="create" class="btn">新增</button>
				<button id="reset" class="btn btn-default">重置</button>

			</div>
		</form>
		 <table id="table" data-toolbar="#toolbar" data-toggle="table" data-row-style="rowStyle" data-classes="table  table-no-bordered"
	           data-minimum-count-columns="2"  data-cache="false" data-pagination="true"  data-striped="data-striped"
	           data-page-list="[5, 10, 20, 50]"  data-query-params="tableQueryParams" style="word-break:break-all;"
	           data-side-pagination="server" data-page-size=10 data-url="./invitation/getInvitationList">
	           <thead>
				<tr class="headTr">
					<th data-field="invitationCode" data-width="35%" >邀请码</th>
					<%--<th data-field="userCode" data-width="20%" >会员ID</th>--%>
					<th data-field="userName" data-width="10%" >客户名称</th>
					<th data-field="isUsed" data-width ="10%" data-formatter="isUsedFormatter">使用情况</th>
					<th data-field="createTime" data-width ="20%" data-formatter="dateFormatter">创建时间</th>
				</tr>
			</thead>
	    </table>
	    <div id="operTemp" style="display:none">
			<ul class="list-inline">
				<li role="presentation"><a class="btn btn-primary">详情</a></li>
			</ul>
		</div>
    </div>
</div>
<div id="createwrap" style="display:none">
	<form id="createform" role="form" class="form-horizontal">
		<div class="form-group" style="text-align: center;">

			<label for="userName1" class="col-sm-3 control-label">客户名称:</label>
			<div class="col-sm-8">
				<input class="form-control" id="userName1" placeholder="请输入邀请码使用的客户名称" onkeyup="filters(this)" onchange="filters(this)"/>
			</div>
			<br>
			<br>

		</div>
	</form>
</div>
<script>

$(function () {
	
	"use strict";
	
	$('#search').on('click', function (e) {
		e.preventDefault();
		searchOrder();
	});
	
});

function tableQueryParams(params) {
	var queryObj = getQueryObject();
	var newParams = $.extend(params,queryObj);
	return newParams;
}
	
function getQueryObject(){
	var queryObj;
	var userName = $("#userName").val();
	var userCode = $("#userCode").val();
	var startVal = $("#startTime").val();
	var endVal = $("#endTime").val();
	
	//设置默认时间为1900-01-01
	var defaultTime = new Date();
	defaultTime.setFullYear(1900,0,1);
	defaultTime.setHours(0);
	defaultTime.setMinutes(0);
	defaultTime.setSeconds(0);
	//日期
	var startTime = new Date(startVal);
	var endTime = new Date(endVal);
	startTime.setHours(0);
	startTime.setMinutes(0);
	startTime.setSeconds(0);
	endTime.setHours(23);
	endTime.setMinutes(59);
	endTime.setSeconds(59);
	
	var todayStart = new Date();
	var todayEnd = new Date();
	todayStart.setHours(0);
	todayStart.setMinutes(0);
	todayStart.setSeconds(0);
	todayEnd.setHours(23);
	todayEnd.setMinutes(59);
	todayEnd.setSeconds(59);
	
	if(startVal == "" || endVal == ""){
		var queryObj = {
			userName : userName,
			userCode : userCode,
			startTime : defaultTime,
			endTime : todayEnd
		}
	}else{
		var queryObj = {
			userName : userName,
			userCode : userCode,
			startTime : startTime,
			endTime : endTime
		}
	}
	
	if(userName ==""){
		delete queryObj.userName;
	}
	
	if(userCode ==""){
		delete queryObj.userCode;
	}
	
	return queryObj;
}
	
function searchOrder(){
	var queryObj = getQueryObject();
	$('#table').bootstrapTable('refresh', {
		query : {
			offset: 0
		},
		silent : true
	});
}

function isUsedFormatter(value, row){
	if(value==null){
		return "-";
	}else if(value==1){
		return "已使用";
	}else if(value==0){
		return "未使用";
	}
}

function dateFormatter(value, row){
	if(value!=null){
		return getSmpFormatDateByLong(value, true);
	}else{
		return '-';
	}
}

$('#create').on('click', function (e) {
	e.preventDefault();
	createAdmin();
});

function createAdmin(){
	$("#createwrap").show();
	$("#createwrap").dialog({
		title   : "新增邀请码",
		'class' :  "mydialog",
		onClose : function() {
			$("#userName1").val('');
			$(this).dialog("close");
		},
		buttons : [{
			text : "提交",
			'class' : "btn btn-primary",
			click : function() {
				var userName1 = $("#userName1").val();
				$.ajax({
					url: "./invitation/addInvitation",
					data: {
						userName : userName1
					},
					type: "POST",
					dataType: 'json',
					cache: false,
					success: function (ret) {
						if (ret.resultCode == 0) {
							$.messager.popup("温馨提示:新增邀请码成功!");
							$('#table').bootstrapTable('refresh', {
								query : {
									offset: 0
								},
								silent : true
							});

						}else{
							$.messager.popup("温馨提示:新增邀请码失败!");
						}
						$(this).dialog("close");
					}
				});
				$("#userName1").val('');
				$(this).dialog("close");
			}
		},{
			text : "关闭",
			'class' : "btn btn-default",
			click : function() {
				$("#userName1").val('');
				$(this).dialog("close");
			}
		}]
	});
}

</script>
</body>
</html>